# -- coding: utf-8 --
#上面这个解决的是中文注释报错问题

def readFile():
    f=file("test.txt")
    for i in  f.readlines():
        print i
        print i.split()  #什么不传，以空格、换行为分隔，转为列表
        print i.strip('\r\n')  #脱掉换行符
        print i.strip('\r\n')
    f.close()

def overwriteFile():
    f=file("test.txt",'w')
    f.write(u"哈哈哈".encode("utf-8"))
    f.flush()
    f.close()

def appendwriteFile():
    f=file("test.txt",'a')
    f.write(u"\n".encode("utf-8"))  #写入换行符
    f.write(u"可口可乐".encode("utf-8"))
    f.flush()
    f.read()
    f.close()

def readFile():
    f=file("test.txt",'a')
    # print f.read()
    # print f.readlines()
    # print f.readline()
    abc=range(10)
    print abc[0]
    abc=[str(i) +'\n' for i in abc]
    print abc[0]
    f.writelines(abc)
    # print f.read()
    # f.flush()
    f.close()

def xreadFoile():
    #f = file("test.txt", 'w+b') +b表示，b表示二进制，中国用于windows，会将windows中换行符转换为linux的换行符
    f = file("test.txt", 'w')
    # f.read()可以读取实时追加的内容，就像linux的tail命令
    f.read();
    #迭代的去读，适合于大文本文件，这样不用等待将文件中所有东西读到内存里
    for i in f.xreadlines():
        print i;

def string():
    name="what is your name"
    print name.find("your")
    print cmp(2,4)
    print cmp(4,2)
    print cmp(2,2)
    print ord('a')
    print cmp('a',97)

def list():
    name_list=['zlex','jack','peter']
    print name_list[2]
    name_list.insert(2,'ttom')
    print name_list
    # 统计jack的次数
    print name_list.count('jack')
    # jack在列表中索引
    print name_list.index('jack')
    print name_list * 2
    # 删除第三个元素
    del name_list[2]
    print name_list
    # 排序
    name_list.sort()
    print name_list
    # 切片，不包含后面
    print name_list[1:3]
    # 取奇偶数,[1::2]表示从第一个元素开始，隔1个取一个
    nums=range(100)
    print nums[1::2]
    print nums[::2]  #等价于nums[0::2]
    # 截断
    nums=[1,2,4,6,2,7,8]
    frist = nums.index(2)
    second=nums.index(2,frist+1)
    print frist,second
    print nums[frist:second]

def tunpleTest():
    import os,sys
    print sys.argv

def directionary():
    name_info={
        "name":"jacky",
        "age":12,
        "job":"programer"
    }
    print name_info['name']
    # 添加一个key-value
    name_info['salary']=3000
    print name_info['salary']
    # 删除字典中key-value
    name_info.pop('job')
    print name_info
    # 循环遍历字典key，效率高
    for k in name_info:
        print k,name_info[k]
    # 循环遍历字典key,value,效率低，先要转成列表，这里转成[('salary', 3000), ('age',12), ('name','jacky')]
    for k,v in name_info.items():
        print k,v



if __name__ == "__main__":
    # appendwriteFile()
    # readFile()
    #2-12-01：:0
    directionary()


